<%@ taglib uri="/WEB-INF/tlds/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/tlds/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/tlds/struts-menu.tld" prefix="menu" %>
<%@ taglib uri="/WEB-INF/tlds/struts-menu-el.tld" prefix="menu-el" %>
<%@ taglib uri="/WEB-INF/tlds/c.tld" prefix="c" %>
<%@ taglib uri="/WEB-INF/tlds/fmt.tld" prefix="fmt" %>
<%@ taglib uri="/WEB-INF/tlds/authz.tld" prefix="authz" %>
<%@ taglib uri="/WEB-INF/tlds/sitemesh-decorator.tld" prefix="decorator"%>
<c:set var="ctx" value="${pageContext.request.contextPath}" scope="request"/>
<script type="text/javascript" src="${ctx}/dwr/interface/ConfigSaverDWR.js"></script>
<script type="text/javascript" src="${ctx}/dwr/engine.js"></script>
<script type="text/javascript" src="${ctx}/dwr/util.js"></script>
<script type="text/javascript">
<!--
	function addCon() {
		var name = $F("name");
		var list = $("connectionList");
		name = trim(name);
		spec = unAllowedChars(name,'<bean:message key="allowedCharacters"/>');
		/* name field must not be empty */
		if(!name || name.length == 0) {
			alert('<bean:message key="error.nameFieldEmpty" />');
		} else if(spec) {
			alert('<bean:message key="error.nameContainsSpecialCharacter" />');
		} else if(contains(list,name)) {
			alert('<bean:message key="error.nameExistsInList"/>');
		} else {
			$('method').value='save';
			$("id").value='';
			$('connectionForm').submit();
		}
	}
	function modifyCon() {
		var name = $F("name");
		var list =  $("connectionList");
		var listVal = $F("connectionList");	
		name = trim(name);
		spec = unAllowedChars(name,'<bean:message key="allowedCharacters"/>');
		if(!listVal || listVal.length==0) {
			alert('<bean:message key="info.selectConnectionToModify"/>');
		} else if(!name || name.length==0) {
			alert('<bean:message key="error.nameFieldEmpty" />');
		} else if(spec) {
			alert('<bean:message key="error.nameContainsSpecialCharacter" />'); 
		} else if(name == $("connectionList")[$("connectionList").selectedIndex].text) {
			alert('<bean:message key="error.nameSameSelectedConnection"/>');
		} else if(contains(list,name)) {
			alert('<bean:message key="error.nameExistsInList"/>'); 
		} else {
			$('method').value='save';
			$('connectionForm').submit();
		}	
	}
	function deleteCon() {
		var listVal = $F("connectionList");
		if(!listVal || listVal.length==0) {
			alert('<bean:message key="info.selectConnectionToDelete"/>');
		} else if(confirm('<bean:message key="info.confirmDeletion"/>')) {
			$('method').value='delete';
			$('connectionForm').submit();
		}
	}
	function selectCon() {
		$("name").value=$("connectionList")[$("connectionList").selectedIndex].text;
		$("id").value=$F("connectionList");	
		$("name").focus();
	}
	
// -->					
</script>
<h1><bean:message key="connectionsPage.title"/></h1>
<div id="subcontent">
	<html:form styleId="connectionForm" action="/connection" focus="name">
		<input type="hidden" name="method" value="init" id="method" />
		<input type="hidden" name="forward" value="connections" />
		<input type="hidden" name="caller" value="connections" />
		<html:hidden styleId="id" property="connection.id" />
		<p><bean:message key="connectionsPage.name" />: <html:text property="connection.name" styleId="name" maxlength="127"/></p>
		<p>
			<select size="6" style="width:60%" name="connectionList" id="connectionList" onchange="selectCon();">
				<c:forEach var="connection" items="${connections}">
					<option value="${connection.id}"><c:out value="${connection.name}" /></option>
				</c:forEach>
			</select>
		</p>
<authz:authorize ifAllGranted="ROLE_SUPERVISOR">
		<p>
			<input name="addConButton" type="submit" value="<bean:message key='connectionsPage.add' />" onclick="addCon();return false;" />&nbsp;
			<input name="modifyConButton" type="submit" value="<bean:message key='connectionsPage.modify' />" onclick="modifyCon();return false;" />&nbsp;
			<input name="delConButton" type="submit" value="<bean:message key='connectionsPage.delete' />" onclick="deleteCon();return false;" />
		</p>
</authz:authorize>
	</html:form>
</div>